/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ValueManipulationWindow.java
 *
 * Created on 28.10.2009, 08:37:56
 */

package at.nhmwien.schema_mapping_tool;

import at.nhmwien.schema_mapping_tool.mappingProcess.*;
import java.util.*;
import javax.swing.*;

/**
 *
 * @author wkoller
 */
public class ManipulationsWindow extends javax.swing.JFrame {

    private String IDInput = null;
    private String IDOutput = null;
    private String defaultTitle = null;
    private DefaultListModel listModel = null;

    /** Creates new form ValueManipulationWindow */
    public ManipulationsWindow() {
        initComponents();

        this.defaultTitle = this.getTitle();
        this.listModel = new DefaultListModel();
        this.manipStackList.setModel(this.listModel);

        String[] manips = ManipulationsHandler.self().getManipulationTypes();
        DefaultComboBoxModel cbModel = new DefaultComboBoxModel();
        for( int i = 0; i < manips.length; i++ ) {
            cbModel.addElement(manips[i]);
        }
        manipTypeComboBox.setModel(cbModel);

        // Get the exists action types
        MappingRecord.existsActionType[] existsActions = MappingRecord.existsActionType.values();
        DefaultComboBoxModel eacbm = new DefaultComboBoxModel();
        this.existsActionComboBox.setModel( eacbm );
        for( int i = 0; i < existsActions.length; i++ ) {
            eacbm.addElement( existsActions[i] );
        }
    }

    public void display( String nIDInput, String nIDOutput ) {
        this.IDInput = nIDInput;
        this.IDOutput = nIDOutput;
        
        this.setTitle( defaultTitle + " - " + IDInput + " <-> " + IDOutput );

        this.listModel.clear();

        // Get any already applied manipulations
        ArrayList<ManipulationRecord> mpList = ManipulationsHandler.self().getManipulations(IDInput, IDOutput);
        if( mpList != null ) {
            Iterator<ManipulationRecord> mpListIt = mpList.iterator();

            while( mpListIt.hasNext() ) {
                ManipulationRecord mRecord = mpListIt.next();

                this.listModel.addElement(mRecord);

                /*HashMap<String,String> params = (HashMap<String,String>) mRecord.parameters;
                regExText.setText(params.get("regExp"));
                outputText.setText(params.get("output"));*/
            }
        }

        // Hide manipSettings by default
        manipSettingsPanel.setVisible(false);
        hintLabel.setVisible( true );

        // Set existsActionType
        if( !IDInput.equals( "*" ) && !IDOutput.equals( "*" ) ) {
            this.existsActionComboBox.setEnabled(true);
            this.persistentMappingCheckBox.setEnabled(true);

            MappingRecord mr = MappingsHandler.Self().getMapping(IDInput, IDOutput);

            this.existsActionComboBox.setSelectedItem( mr.existsAction );
            this.persistentMappingCheckBox.setSelected( mr.persistentMapping );
        }
        else {
            this.existsActionComboBox.setEnabled(false);
            this.persistentMappingCheckBox.setEnabled(false);
            //this.mappingSettingsPanel.setEnabled(false);
        }

        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        settingsPanel = new javax.swing.JPanel();
        manipSettingsPanel = new javax.swing.JPanel();
        regExpPanel = new javax.swing.JPanel();
        regExLabel = new javax.swing.JLabel();
        regExText = new javax.swing.JTextField();
        outputLabel = new javax.swing.JLabel();
        outputText = new javax.swing.JTextField();
        manipTypeComboBox = new javax.swing.JComboBox();
        applyButton = new javax.swing.JButton();
        trimCheckBox = new javax.swing.JCheckBox();
        hintLabel = new javax.swing.JLabel();
        cancelButton = new javax.swing.JButton();
        manipStackPanel = new javax.swing.JPanel();
        manipStackPane = new javax.swing.JScrollPane();
        manipStackList = new javax.swing.JList();
        removeManipButton = new javax.swing.JButton();
        addManipButton = new javax.swing.JButton();
        sortUpButton = new javax.swing.JButton();
        sortDownButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        mappingSettingsPanel = new javax.swing.JPanel();
        existsActionLabel = new javax.swing.JLabel();
        existsActionComboBox = new javax.swing.JComboBox();
        persistentMappingCheckBox = new javax.swing.JCheckBox();

        setTitle("Manipulate Record Value");
        setResizable(false);

        settingsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Manipulation Settings"));

        regExLabel.setText("Expression:");

        outputLabel.setText("Output:");

        javax.swing.GroupLayout regExpPanelLayout = new javax.swing.GroupLayout(regExpPanel);
        regExpPanel.setLayout(regExpPanelLayout);
        regExpPanelLayout.setHorizontalGroup(
            regExpPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(regExpPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(regExpPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(outputLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(regExLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(regExpPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(outputText, javax.swing.GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE)
                    .addComponent(regExText, javax.swing.GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE))
                .addContainerGap())
        );
        regExpPanelLayout.setVerticalGroup(
            regExpPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(regExpPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(regExpPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(regExLabel)
                    .addComponent(regExText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(regExpPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(outputLabel)
                    .addComponent(outputText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        applyButton.setText("Apply");
        applyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                applyButtonActionPerformed(evt);
            }
        });

        trimCheckBox.setText("Trim String after processing");

        javax.swing.GroupLayout manipSettingsPanelLayout = new javax.swing.GroupLayout(manipSettingsPanel);
        manipSettingsPanel.setLayout(manipSettingsPanelLayout);
        manipSettingsPanelLayout.setHorizontalGroup(
            manipSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(manipSettingsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(manipSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(manipTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(regExpPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(applyButton, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(trimCheckBox))
                .addContainerGap())
        );
        manipSettingsPanelLayout.setVerticalGroup(
            manipSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(manipSettingsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(manipTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(regExpPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(trimCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(applyButton)
                .addContainerGap())
        );

        hintLabel.setFont(new java.awt.Font("Tahoma", 1, 14));
        hintLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        hintLabel.setText("Please select a Manipulation Entry from the right ->");

        javax.swing.GroupLayout settingsPanelLayout = new javax.swing.GroupLayout(settingsPanel);
        settingsPanel.setLayout(settingsPanelLayout);
        settingsPanelLayout.setHorizontalGroup(
            settingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(settingsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(settingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(manipSettingsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(hintLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE))
                .addContainerGap())
        );
        settingsPanelLayout.setVerticalGroup(
            settingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(settingsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(manipSettingsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(hintLabel)
                .addContainerGap(61, Short.MAX_VALUE))
        );

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        manipStackPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Manipulations Stack"));

        manipStackPane.setEnabled(false);
        manipStackPane.setFocusable(false);

        manipStackList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                manipStackListValueChanged(evt);
            }
        });
        manipStackPane.setViewportView(manipStackList);

        removeManipButton.setText("-");
        removeManipButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeManipButtonActionPerformed(evt);
            }
        });

        addManipButton.setText("+");
        addManipButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addManipButtonActionPerformed(evt);
            }
        });

        sortUpButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/at/nhmwien/schema_mapping_tool/images/arrow_up.png"))); // NOI18N
        sortUpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sortUpButtonActionPerformed(evt);
            }
        });

        sortDownButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/at/nhmwien/schema_mapping_tool/images/arrow_down.png"))); // NOI18N
        sortDownButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sortDownButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout manipStackPanelLayout = new javax.swing.GroupLayout(manipStackPanel);
        manipStackPanel.setLayout(manipStackPanelLayout);
        manipStackPanelLayout.setHorizontalGroup(
            manipStackPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(manipStackPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(manipStackPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(manipStackPane, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(manipStackPanelLayout.createSequentialGroup()
                        .addComponent(removeManipButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(addManipButton)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(manipStackPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(sortUpButton)
                    .addComponent(sortDownButton))
                .addContainerGap())
        );
        manipStackPanelLayout.setVerticalGroup(
            manipStackPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(manipStackPanelLayout.createSequentialGroup()
                .addGroup(manipStackPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, manipStackPanelLayout.createSequentialGroup()
                        .addComponent(manipStackPane, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(manipStackPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(removeManipButton)
                            .addComponent(addManipButton)))
                    .addGroup(manipStackPanelLayout.createSequentialGroup()
                        .addComponent(sortUpButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(sortDownButton)))
                .addContainerGap())
        );

        saveButton.setText("Save");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        mappingSettingsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Mapping Settings"));

        existsActionLabel.setText("Action when mapping already contains an entry:");

        persistentMappingCheckBox.setText("Persistent Mapping");

        javax.swing.GroupLayout mappingSettingsPanelLayout = new javax.swing.GroupLayout(mappingSettingsPanel);
        mappingSettingsPanel.setLayout(mappingSettingsPanelLayout);
        mappingSettingsPanelLayout.setHorizontalGroup(
            mappingSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mappingSettingsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mappingSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mappingSettingsPanelLayout.createSequentialGroup()
                        .addComponent(existsActionLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 170, Short.MAX_VALUE)
                        .addComponent(existsActionComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(persistentMappingCheckBox))
                .addContainerGap())
        );
        mappingSettingsPanelLayout.setVerticalGroup(
            mappingSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mappingSettingsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mappingSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(existsActionLabel)
                    .addComponent(existsActionComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(persistentMappingCheckBox)
                .addContainerGap(60, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(mappingSettingsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(settingsPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(manipStackPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cancelButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 566, Short.MAX_VALUE)
                        .addComponent(saveButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(manipStackPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(settingsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(mappingSettingsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cancelButton)
                    .addComponent(saveButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void applyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_applyButtonActionPerformed
        /*HashMap<String,String> params = new HashMap<String,String>();
        params.put("expression", regExText.getText());
        params.put("output", outputText.getText());
        
        ManipulationsHandler.self().addManipulation(this.IDInput, this.IDOutput, (String) manipTypeComboBox.getSelectedItem(), params);
        this.setVisible(false);*/

        // Retrieve record
        ManipulationRecord mRecord = (ManipulationRecord) this.manipStackList.getSelectedValue();
        HashMap<String,String> params = (HashMap<String,String>) mRecord.parameters;

        // Manipulate record
        mRecord.type = (String) manipTypeComboBox.getSelectedItem();
        params.put("expression", regExText.getText());
        params.put("output", outputText.getText());

        // Check if the string should be trimmed
        if( trimCheckBox.isSelected() ) {
            mRecord.options.put( "trim", "true" );
        }
        else {
            mRecord.options.remove( "trim" );
        }
    }//GEN-LAST:event_applyButtonActionPerformed

    private void addManipButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addManipButtonActionPerformed
        ManipulationRecord mRecord = new ManipulationRecord();
        mRecord.type = ManipulationsHandler.self().getManipulationTypes()[0];
        HashMap<String,String> params = new HashMap<String,String>();
        params.put("expression", "");
        params.put("output", "");
        mRecord.parameters = params;

        this.listModel.addElement(mRecord);
    }//GEN-LAST:event_addManipButtonActionPerformed

    private void removeManipButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeManipButtonActionPerformed
        if( this.manipStackList.getSelectedIndex() != -1 ) {
            this.listModel.remove(this.manipStackList.getSelectedIndex());
        }
    }//GEN-LAST:event_removeManipButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        ArrayList<ManipulationRecord> mRecords = new ArrayList<ManipulationRecord>(0);

        // Prepare manipulation-record stack
        for( int i = 0; i < this.listModel.size(); i++ ) {
            mRecords.add( (ManipulationRecord) this.listModel.getElementAt(i) );
        }

        ManipulationsHandler.self().setManipulations(IDInput, IDOutput, mRecords);

        //MappingsHandler.self().manipulateMapping(IDInput, IDOutput, (MappingRecord.existsActionType) this.existsActionComboBox.getSelectedItem() );
        // Update mapping record with current settings
        if( !IDInput.equals( "*" ) && !IDOutput.equals( "*" ) ) {
            MappingRecord mr = MappingsHandler.Self().getMapping(IDInput, IDOutput);
            mr.existsAction = (MappingRecord.existsActionType) this.existsActionComboBox.getSelectedItem();
            mr.persistentMapping = this.persistentMappingCheckBox.isSelected();
            MappingsHandler.Self().setMapping(IDInput, IDOutput, mr);
        }

        this.setVisible(false);
    }//GEN-LAST:event_saveButtonActionPerformed

    private void sortUpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sortUpButtonActionPerformed
        ManipulationRecord mprTemp = null;
        int selectedIndex = this.manipStackList.getSelectedIndex();

        if( selectedIndex > 0 ) {
            mprTemp = (ManipulationRecord) this.listModel.getElementAt(selectedIndex);
            this.listModel.setElementAt(this.listModel.getElementAt(selectedIndex - 1), selectedIndex);
            this.listModel.setElementAt(mprTemp, selectedIndex - 1);

            this.manipStackList.setSelectedIndex(selectedIndex - 1);
        }
    }//GEN-LAST:event_sortUpButtonActionPerformed

    private void sortDownButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sortDownButtonActionPerformed
        ManipulationRecord mprTemp = null;
        int selectedIndex = this.manipStackList.getSelectedIndex();

        if( selectedIndex >= 0 ) {
            mprTemp = (ManipulationRecord) this.listModel.getElementAt(selectedIndex);
            this.listModel.setElementAt(this.listModel.getElementAt(selectedIndex + 1), selectedIndex);
            this.listModel.setElementAt(mprTemp, selectedIndex + 1);

            this.manipStackList.setSelectedIndex(selectedIndex + 1);
        }
    }//GEN-LAST:event_sortDownButtonActionPerformed

    private void manipStackListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_manipStackListValueChanged
        if (!evt.getValueIsAdjusting()) {
            if( manipStackList.getSelectedValue() == null ) {
                manipSettingsPanel.setVisible(false);
                hintLabel.setVisible(true);
            }
            else {
                ManipulationRecord mRecord = (ManipulationRecord) manipStackList.getSelectedValue();
                HashMap<String, String> params = (HashMap<String, String>) mRecord.parameters;

                regExText.setText(params.get("expression"));
                outputText.setText(params.get("output"));
                manipTypeComboBox.setSelectedItem(mRecord.type);
                trimCheckBox.setSelected(mRecord.options.containsKey("trim"));

                manipSettingsPanel.setVisible(true);
                hintLabel.setVisible(false);
            }
        }
    }//GEN-LAST:event_manipStackListValueChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addManipButton;
    private javax.swing.JButton applyButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JComboBox existsActionComboBox;
    private javax.swing.JLabel existsActionLabel;
    private javax.swing.JLabel hintLabel;
    private javax.swing.JPanel manipSettingsPanel;
    private javax.swing.JList manipStackList;
    private javax.swing.JScrollPane manipStackPane;
    private javax.swing.JPanel manipStackPanel;
    private javax.swing.JComboBox manipTypeComboBox;
    private javax.swing.JPanel mappingSettingsPanel;
    private javax.swing.JLabel outputLabel;
    private javax.swing.JTextField outputText;
    private javax.swing.JCheckBox persistentMappingCheckBox;
    private javax.swing.JLabel regExLabel;
    private javax.swing.JTextField regExText;
    private javax.swing.JPanel regExpPanel;
    private javax.swing.JButton removeManipButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JPanel settingsPanel;
    private javax.swing.JButton sortDownButton;
    private javax.swing.JButton sortUpButton;
    private javax.swing.JCheckBox trimCheckBox;
    // End of variables declaration//GEN-END:variables

}
